package com.fast.manager.system.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fast.commons.web.BaseController;
import com.fast.commons.web.model.AjaxResponse;
import com.fast.commons.web.model.DataGrid;
import com.fast.manager.system.domain.DataDict;
import com.fast.manager.system.domain.DataDictDetail;
import com.fast.manager.system.service.DataDictDetailService;
import com.fast.manager.system.service.DataDictService;

@Controller
@RequestMapping("/manager/system/datadict")
public class DataDictController extends BaseController {
	
	@Autowired
	private DataDictService service ;
	
	@Autowired
	private DataDictDetailService detailService ;

	/**
	 * 主页
	 * @return
	 */
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main(Model model) {
		return viewName("main") ;
	}
	
	/**
	 * 表单页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/form", method=RequestMethod.GET)
	public String form(Long id, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		return viewName("form") ;
	}
	
	/**
	 * 添加
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse add(DataDict dto) {
		return this.service.add(dto);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete/{id}", method=RequestMethod.DELETE)
	public @ResponseBody AjaxResponse delete(@PathVariable Integer id) {
		return this.service.delete(id) ;
	}
	
	/**
	 * 修改
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse update(DataDict dto) {
		return this.service.update(dto);
	}
	
	/**
	 * 根据ID获取数据
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/get/{id}", method=RequestMethod.GET)
	public @ResponseBody DataDict get(@PathVariable Long id) {
		return this.service.get(id) ;
	}
	
	/**
	 * 字典管理
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/tree", method=RequestMethod.GET)
	public @ResponseBody List<DataDict> tree(DataDict dto, HttpServletRequest request) {
		return this.service.tree();
	}
	
	
	
	/**********************************字典详细**********************************/
	
	/**
	 * 表单页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/form_detail", method=RequestMethod.GET)
	public String form_detail(Long id, Long dataDictId, Model model) {
		if(null != dataDictId) {
			model.addAttribute("dataDictId", dataDictId) ;
		}
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		return viewName("form_detail") ;
	}
	
	/**
	 * 添加
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/add_detail", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse add_detail(DataDictDetail dto) {
		return this.detailService.add(dto);
	}
	
	/**
	 * 删除
	 * @param ids 数组
	 * @return
	 */
	@RequestMapping(value="/delete_detail/{id}", method=RequestMethod.DELETE)
	public @ResponseBody AjaxResponse delete_detail(@PathVariable Integer id) {
		return this.detailService.delete(id) ;
	}
	
	/**
	 * 删除
	 * @param ids 数组
	 * @return
	 */
	@RequestMapping(value="/delete_detail_Batch", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse delete_detail_Batch(Long[] ids) {
		return this.detailService.deleteBatch(ids);
	}
	
	/**
	 * 修改
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/update_detail", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse update_detail(DataDictDetail dto) {
		return this.detailService.update(dto);
	}
	
	/**
	 * 根据ID获取数据
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/get_detail/{id}", method=RequestMethod.GET)
	public @ResponseBody DataDictDetail get_detail(@PathVariable Long id) {
		return this.detailService.get(id) ;
	}
	
	/**
	 * 字典详细管理 树状表格
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/treegrid/{dataDictId}", method=RequestMethod.GET)
	public @ResponseBody List<DataDictDetail> treegrid(@PathVariable Integer dataDictId) {
		return this.detailService.treegrid(dataDictId);
	}
	
	/**
	 * 字典详细管理 表格
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/datagrid/{dataDictId}", method=RequestMethod.GET)
	public @ResponseBody DataGrid<DataDictDetail> datagrid(@PathVariable Integer dataDictId) {
		return this.detailService.datagrid(dataDictId);
	}
	
	/**
	 * Combobox使用
	 * @param dict_code
	 * @return
	 */
	@RequestMapping(value="/getCombobox/{dict_code}", method=RequestMethod.GET)
	public @ResponseBody List<DataDictDetail> getCombobox(@PathVariable String dict_code) {
		return this.detailService.getDictCombobox(dict_code);
	}
}
